package org.seven.jrdp.web.mgt.sys;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.seven.jrdp.commons.constant.GlobalContants;
import org.seven.jrdp.commons.model.Result;
import org.seven.jrdp.commons.util.ServletUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/mgt/sys/home")
public class SysHomeController {
	private static final String HOME_PATH = "/static/html/mgt/core/home.html";

	@RequestMapping("/info")
	public ModelAndView info() throws IOException {
		ModelAndView modelAndView = new ModelAndView("mgt/sys/home/home_info");
		InputStream in = new FileInputStream(ServletUtils.getServletContext().getRealPath(HOME_PATH));
		modelAndView.addObject("content", IOUtils.toString(in, GlobalContants.ENCODING));
		in.close();
		return modelAndView;
	}

	@RequestMapping("/find")
	public ModelAndView find() throws IOException {
		ModelAndView modelAndView = new ModelAndView("mgt/sys/home/home_find");
		InputStream in = new FileInputStream(ServletUtils.getServletContext().getRealPath(HOME_PATH));
		modelAndView.addObject("content", IOUtils.toString(in, GlobalContants.ENCODING));
		in.close();
		return modelAndView;
	}

	@RequestMapping("/save")
	public Result save(String content, HttpServletRequest request) throws IOException {
		OutputStream out = new FileOutputStream(ServletUtils.getServletContext().getRealPath(HOME_PATH));
		IOUtils.write(content, out, GlobalContants.ENCODING);
		out.close();
		return Result.SUCCESS;
	}
}